package com.supermap.dataservice.cloud.cust.user;

import com.alibaba.dubbo.config.annotation.Service;
import com.supermap.dataservice.cloud.cust.menu.SysMenuMapper;
import com.supermap.dataservice.cloud.cust.menu.SysMenuServiceImpl;
import com.supermap.dataservice.cloud.cust.role.SysAreaRoleMapper;
import com.supermap.dataservice.mysql.config.DynamicDataSourceContextHolder;
import com.supermap.dataservice.util.JSONUtil;
import com.supermap.dubbo.api.cloud.cust.role.TreeNodeList;
import com.supermap.dubbo.api.cloud.cust.user.SysUserService;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author aqua
 * @desc
 * @date 2019年03月04 15:14
 * @email 1301584033@qq.com
 */
@Service
public class SysUserServiceImpl implements SysUserService {

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private SysAreaRoleMapper sysAreaRoleMapper;
    @Resource
    private SysMenuMapper sysMenuMapper;

    @Override
    public JSONUtil getUserTreeAreaRole(String areaCode, Integer userId) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        TreeNodeList treeNodeList=new TreeNodeList();
        treeNodeList.setName("全部节点");
        treeNodeList.setOpen(true);

        List<TreeNodeList> treeNodeList1=sysAreaRoleMapper.getAllAreaByAreaCode(areaCode);
        for(TreeNodeList nodeList:treeNodeList1){
            List<TreeNodeList> treeNodeLists= sysUserMapper.getAllAreaRoleByRoleId(areaCode,userId);
            for(TreeNodeList treeNodeList2:treeNodeLists){
                if(treeNodeList2.getState()>0){
                    treeNodeList2.setChecked(true);
                }
            }
            nodeList.setChildren(treeNodeLists);
        }
        treeNodeList.setChildren(treeNodeList1);
        return new JSONUtil().setData(treeNodeList1);
    }

    @Override
    public JSONUtil putUserAreaRole(String userId,String areaCode, String roleId) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        //删除用户相关角色
        sysUserMapper.delUserAreaRole(Integer.valueOf(userId));
        String roleIds[]=roleId.split(",");
        for(String str:roleIds){
            sysUserMapper.putUserAreaRole(Integer.valueOf(userId),Integer.valueOf(str));
            //删除区域菜单
            sysUserMapper.delUserMenu(Integer.valueOf(userId));
            //查询区域角色菜单入库
            List<Integer> list=sysUserMapper.getAreaRole(Integer.valueOf(str),areaCode);
            //入库
            sysUserMapper.putMenuI(list,Integer.valueOf(userId));
            //查询定制菜单入库
            list=sysUserMapper.getSysMenuId(Integer.valueOf(userId));
            for(Integer menuid:list){
                putCostomMenu(menuid,Integer.valueOf(userId));
            }

        }

        return new JSONUtil();
    }

    public JSONUtil putCostomMenu(Integer menuid, Integer userId) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        //添加目录
        //通过menuid取Code
        new SysMenuServiceImpl().foundMenu(menuid,userId);
        sysMenuMapper.putSysMenu(menuid,userId);
        return new JSONUtil();
    }



}
